<?php
/**
 * search.php
 */

require_once ("./include/common.inc.php");
/*
 * 搜索条件设定
 */
//最大返回结果
$maxsearchresults = 50;
//最大关键字数
$maxkeywords = 20;
//每页条数
$perpage = 10;

//-------------------------------
function searchFilter($str) {
	//转义引号
	if(!get_magic_quotes_gpc()) {
		$str = addslashes($str);
	}
	
	//过滤MySQL特殊符号
	$str = str_replace('[', '[[]', $str);
	$str = str_replace('_', '[_]', $str);
	$str = str_replace('%', '[%]', $str);
	
	//多个关键字
	if(preg_match('/ /', $str)) {
		$str_arr = array();
		$str_arr = explode(" ", $str);
		return $str_arr;
	}else{
	//一个关键字
		return $str;
	}
}

if($dosubmit)
{
	$keywords = $_POST['keywords'] ? trim($_POST['keywords']) : '';

	//if(empty($cateid)) showmessage("您需要选择分类再搜索");
	if(empty($keywords)) showmessage("搜索的关键字不能为空");
	
	//搜索字数限制
	$maxkeywords = $maxkeywords ? $maxkeywords : 20;
	if(strlen($keywords) > $maxkeywords) showMessage("您输入的关键字太长了，精简一下或许会有更好的效果！");

	//搜索返回最大结果数
	$maxsearchresults = $maxsearchresults ? intval($maxsearchresults) : 500;
	
	//对关键字进行过滤
	$keywordss = searchFilter($keywords);
	if(is_array($keywordss)) {
		$key_count = count($keywordss);
		$kw = '';
		for($i=0; $i<$key_count; $i++)
		{
			$kw .= " AND title LIKE '%$keywordss[$i]%'";
		}
	}
	else
	{
		$key_count = '1';
		$kw = " AND title LIKE '%$keywordss%'";
	}
	$sql = $where = '';
	$sql = "SELECT title, url, pubtime FROM {$tablepre}content WHERE status>0 $where $kw ORDER BY pubtime DESC LIMIT 0, $maxsearchresults";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$data[] = $row;
	}
}
include template("search");
?>